import org.omg.CORBA.PUBLIC_MEMBER;

import javax.crypto.spec.PSource;
import java.lang.reflect.Array;
import java.util.Arrays;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    //改变原有数组元素的值
    public static void main(String[] args) {
        int[] array = new int[4];
        array[0] = 1;
        for (int i = 1; i < array.length; i++) {
            array[i] = array[i-1] +1;
        }

        transform(array);
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }

        System.out.println(avg(array));
        int[] array2 = {1,2,3,4,5,6};
        fun(array2);
        System.out.println();
        int[] array3 = {1,2,3,0,5,6};
        System.out.println(fun2(array3,6));

        int[] num = fun3(array3,9);
        for (int i = 0; i < num.length; i++) {
            System.out.print(num[i]+" ");
        }
        System.out.println();
        int[] array4 = {1,2,3,2,1};
        System.out.println(fun4(array4));
        int[] array5 = {1,2,3,2,2};
        System.out.println(fun5(array5));


        int[] array6 = {1,3,5,2,1,4,7};
        System.out.println(fun6(array6));

    }
    //实现一个方法 transform,
    // 以数组为参数, 循环将数组中的每个元素 乘以 2 , 并设置到对应的数组元素上
    public static void transform(int[] array){
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i]*2;
        }
    }
    //实现一个方法 avg, 以数组为参数, 求数组中所有元素的平均值
    public static int avg(int[] array){
        int sum = 0;
        int le = array.length;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        int count = sum/le;
        return count;

    }

    //奇数位于偶数之前
    public static void fun(int[] array){
        //给数组元素套一个下标
        for (int i = 0; i < array.length-1; i++) {
            //读取数组内元素的值
            for (int j = 0; j < array.length-1; j++) {
                //判断是偶数还是奇数，如果是奇数，则需要将奇数放在偶数之前
                if (array[j]%2 == 0){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                }
            }
        }
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
    }


    //给定一个有序整型数组, 实现二分查找
    public static int fun2(int[] array,int key) {
        int left = 0;
        int right = array.length-1;
        while (left <= right){
            int mid = (left+right)/2;
            if (array[mid] == key){
                return mid;
            } else if (array[mid] < key) {
                left = mid + 1;
            }else {
                right = mid - 1;
            }

        }//没有找到这个数值
         return -1;
    }

    //两数之和
    public static int[] fun3(int[] array,int tag) {
        int[] res = new int[2];
        for (int i = 0; i < array.length-1; i++) {
            for (int j = array.length-1; j > i ; j--) {
                if (array[i] + array[j] == tag){
                    res[0] = i;
                    res[1] = j;
                }
            }
        }
        return res;
    }

    //只出现一次的数字
    public static int fun4(int[] array){
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            count ^= array[i];
        }
        return count;
    }

    //多数元素
    public static int fun5(int[] array){
        Arrays.sort(array);
        return array[array.length/2];
    }

    //存在连续三个奇数的数组
    public static boolean fun6(int[] array){
        int count = 0;//记录奇数的个数
        for (int i = 0; i < array.length; i++) {
            if (fun7(array[i])){
                count++;
                if (count == 3){
                    return true;
                }
            }else {
                count = 0;
            }
        }
        return false;
    }
    public static boolean fun7(int n){
        return n % 2 !=0 ;
    }
}